package org.stvd.controller.user;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.Consts;
import org.stvd.common.aspectj.lang.annotation.Log;
import org.stvd.common.aspectj.lang.enums.BusinessType;
import org.stvd.common.enums.UserLoginTypeEnum;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.common.utils.encode.Base64Util;
import org.stvd.common.utils.encode.RSAUtil;
import org.stvd.service.admin.UsersService;


/**
 * 账户注册-控制类
 * @author houzx
 */
@Controller
public class RegistController{

    @Autowired
    private UsersService usersService; //账户信息Service
    
    private String ftlURL = "/user/Register.html";
    
    private static final Logger logger = Logger.getLogger(RegistController.class);
    
    /**
     * @Description: 用户注册
     * @param map
     * @param request
     * @return
     */
    @GetMapping(value = "/regist")
    public String onRegistLoad(ModelMap map, HttpServletRequest request) {
        //获取公钥信息
        String path = this.getClass().getResource("/rsakey/publicKey.txt").getFile();
        String publicKey = RSAUtil.readTxt(path);
        map.put("publicKey", publicKey);
        return ftlURL;
    }
    
    @Log(title = "用户账号注册", businessType = BusinessType.INSERT)
    @PostMapping(value = "/regist")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> onRegistAction(HttpServletRequest request,ModelMap map) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<>();
        //获取提交数据
        String loginCode = request.getParameter("logincode");
        String passWord = request.getParameter("password");
        String confirmPass = request.getParameter("confirmpass");
        String validateCode = request.getParameter("validatecode");
        
        //获取验证码并校验
        Object obj = null ;
        HttpSession session = request.getSession(false); 
        if (session != null) { 
            obj = session.getAttribute("validateCode");
        }
        String confirmCode = obj == null ? "" : obj.toString();
        if(!confirmCode.equalsIgnoreCase(validateCode)){
            result.setError("验证码不正确！");
            return result;
        }
        try {
            //RSA解密
            String privateKey = RSAUtil.readTxt(this.getClass().getResource("/rsakey/privateKey.txt").getFile());
            passWord = passWord.replaceAll("%2B","+");
            byte[] decryptPassWord = RSAUtil.decryptByPrivateKey(Base64Util.decode(passWord), privateKey);
            passWord = new String(decryptPassWord);
            confirmPass = confirmPass.replaceAll("%2B","+");
            byte[] decryptComfirmPass = RSAUtil.decryptByPrivateKey(Base64Util.decode(confirmPass), privateKey);
            confirmPass = new String(decryptComfirmPass);
            result.transferResult(usersService.registAccount(loginCode, UserLoginTypeEnum.UserCode.getValue(), 
                loginCode, validateCode, passWord, confirmPass, Consts.DEFAULT_USER_ROLE_CODE, Consts.DEFAULT_USER_DEP_CODE, Consts.GUID, Consts.SYSTEM_URL));
        } catch (Exception e) {
            e.printStackTrace();
            result.setError("请求数据异常！");
            logger.error("RSA解密异常！失败原因：" + e);
            return result;
        }
        result.setMsg("恭喜您注册成功！");
        return result;
    }
}
